package UI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import Dao.StudentDao;
import Dao.jdbcTools;
import Model.Student;
import Model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.io.PrintWriter;


@WebServlet("/updateOne")
public class UpdataData extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
//        HttpSession session = req.getSession();
//        if (session.getAttribute("user")==null){
//            PrintWriter writer = resp.getWriter();
//            writer.write("<script>" +
//                    "alert('请正常登录！');" +
//                    "window.location.href='login.jsp'" +
//                    "</script>");
//        }
        PrintWriter writer = resp.getWriter();
        //Student stu =(Student) req.getAttribute("stu");
        String id = req.getParameter("id");
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = factory.openSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student stu = studentDao.findById(Integer.valueOf(id));
        // Student stu = jdbcTools.select("select * from stuhhh where id=?", Student.class,id);

        System.out.println("bscjagjgkc");
        writer.write("<!doctype html>");
        writer.write("<html>");
        writer.write("<head>");
        writer.write("<meta charset='utf-8'>");
        writer.write("</head>");
        writer.write("<h1>学生信息更新展示</h1>");
        writer.write("<body style='backgroud:#ccffff;'>");
        writer.write("<h1>学生信息展示</h1>");
        writer.write("<div style='margin:0 auto;width:600px;border:solid 3px red;'>" +
                "<h3><a href='updateOne'>修改信息</a></h3>");
        writer.write("<form action='updateok' methon='post'>" +
                "iD:<input type='hidden' name='id' value=" +stu.getId()+
                "><br>"+
                "姓名:<input type='text' name='name' value=" +stu.getName()+
                "><br>"+
                "性别:<input type='text' name='sex' value=" +stu.getSex()+
                "><br>" +
                "年龄:<input type='text' name='age' value=" +stu.getAge()+
                "><br>" +
                "专业:<input type='text' name='major' value=" +stu.getMajor()+
                "><br>" +
                "时间:<input type='text' name='time' value=" +stu.getTime()+
                "><br>" +
                "<button>提交</button>" +
                "<input type='reset' value='重置'/>" +
                "</form>" +
                "</div>");
        writer.write("</body>");
        writer.write("</html>");


    }
}
